package com.tianyang.yukuaifuzs.business.auth.presenter;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifuzs.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifuzs.business.auth.contract.AuthSelectTypeContract;
import com.tianyang.yukuaifuzs.business.auth.model.AuthModel;
import com.tianyang.yukuaifuzs.business.auth.provider.AuthOpenProvider;
import com.tianyang.yukuaifuzs.business.login.bean.LoginOutput;
import com.tianyang.yukuaifuzs.business.main.bean.ChannelOutput;
import com.tianyang.yukuaifuzs.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifuzs.business.main.model.MainModel;
import com.tianyang.yukuaifuzs.common.cache.CacheManage;
import com.tianyang.yukuaifuzs.common.cache.CacheModel;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：商户进件选择类型
 * Created by xyj on 2024/3/7
 */
public class AuthSelectTypePresenter implements AuthSelectTypeContract.Presenter {

    private AuthSelectTypeContract.View mView;

    private CompositeSubscription mSubscription;

    private String posStatus, inviteCode;

    public AuthSelectTypePresenter(AuthSelectTypeContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (output != null) {
            inviteCode = output.getInvitecode();
            if (output.getPosStatus() < 0) {
                posStatus = output.getPosStatus() + "";
            } else if (output.getPosStatus() == 0) {
                posStatus = "1";
            }
        }
    }

    @Override
    public void getChannelList() {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().getChannelList()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ChannelOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<ChannelOutput> outputs) {
                        if (outputs != null && !outputs.isEmpty()) {
                            mView.showChannelView(outputs);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onStartAuthClick(String merchantType, String userName, boolean isAgent, String posChannel) {
        if (isAgent) {
            mView.showLoadingDialog(true);
            Subscription subscription = AuthModel.getInstance()
                    .userRegister(userName, inviteCode, posChannel)
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<LoginOutput>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            mView.dismissLoadingDialog();
                            mView.showErrorText(e);
                        }

                        @Override
                        public void onNext(LoginOutput loginOutput) {
                            saveIdCertTemp(merchantType, userName);
                        }
                    });
            mSubscription.add(subscription);
        } else {
            openAuthActivity(merchantType, userName);
        }
    }

    private void saveIdCertTemp(String merchantType, String userName) {
        Subscription subscription = AuthModel.getInstance().saveIdCertTemp(userName, merchantType)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(JSONObject object) {
                        openAuthActivity(merchantType, userName);
                    }
                });
        mSubscription.add(subscription);
    }

    private void openAuthActivity(String merchantType, String userName) {
        switch (merchantType) {
            case AuthConfig.MCH_TYPE_SMALL:
                AuthOpenProvider.getInstance().openAuthMerchantActivity(mView.getContext(), userName, posStatus, false, true);
                break;
            case AuthConfig.MCH_TYPE_INDIVIDUAL:
                AuthOpenProvider.getInstance().openAuthIndividualActivity(mView.getContext(), userName, posStatus, false, true);
                break;
            case AuthConfig.MCH_TYPE_COMPANY:
                AuthOpenProvider.getInstance().openAuthCompanyActivity(mView.getContext(), userName, posStatus, false, true);
                break;
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
